using System;
using System.Collections.Generic;
using System.Text;
using Pixysoft.TestDrivens;
using Pixysoft.Framework.Logs.Core;
using System.IO;

namespace Pixysoft.Framework.Logs.TestCases
{
    [TestDriven(TestDrivenType.IntegrationTest, "CoreLogger")]
    class UnitTest_001_CoreLogger
    {
        private static IAssert assert = Assert.New(true);

        [TestStart]
        public static bool TestCaseStart()
        {
            return true;
        }

        [TestStop]
        public static void TestCaseStop()
        {
        }

        [TestStateSave]
        public static ITestDrivenDocument TestCaseStateSave()
        {
            return assert.Document;
        }

        [TestCase("Unpackage")]
        public static void TestCase_000_Unpackage()
        {
            Assert.Call(UnitTest_019_Recorder.TestCase_000_Append);

            string path = StringFilter.RegularPath(AppDomain.CurrentDomain.BaseDirectory);
            string filename = "pixysoft";
            string postfix = ".log";

            CoreLogger logger = new CoreLogger(filename, null);
            logger.Setting.LogPath = path;

            ILogPackage package = logger.Package();
            assert.Write(package.CreateDate);
            assert.Write(package.DateFrom);
            assert.Write(package.DateTo);
            assert.Write(package.Destination);
            foreach (string src in package.Sources)
                assert.Write(src);
            assert.Write(logger.CountOfFiles());
            assert.Write(logger.CountOfFiles(1));

            assert.Write(logger.Info("hello<>&$#@!${}[];'"));
            assert.Write(logger.Debug("hello<>&$#@!${}[];'"));
            assert.Write(logger.Warn("hello<>&$#@!${}[];'"));
            assert.Write(logger.Error("hello<>&$#@!${}[];'"));
            assert.Write(logger.Fatal("hello<>&$#@!${}[];'"));
            try
            {
                throw new Exception("hello<>&$#@!${}[];'");
            }
            catch (Exception ex)
            {
                assert.Write(logger.Fatal(ex));
            }
            assert.IsEqual(path, logger.Setting.LogPath);
            assert.IsEqual(filename, logger.Setting.Filename);
            logger.Close();
            assert.Write(File.ReadAllText(path + filename + ".log"));

            logger.Close();

            logger.Format();
            logger.Format(true);

            assert.IsTrue(SingletonLogger.Backup(path, filename, postfix));
            assert.IsEqual(1, logger.CountOfFiles());
            assert.IsEqual(1, logger.CountOfFiles(1));

            package = logger.Package();
            assert.Write(package.CreateDate);
            assert.Write(package.DateFrom);
            assert.Write(package.DateTo);
            assert.Write(package.Destination);
            foreach (string src in package.Sources)
                assert.Write(src);

        }

        [TestCase("GetRecentReport")]
        public static void TestCase_006_GetRecentReport()
        {
            string filename = "pixysoft";
            string postfix = ".log";

            CoreLogger logger = new CoreLogger(filename, null);
            SingletonLogger.Backup(logger.Setting.LogPath, filename, postfix);

            string id = logger.Info("hello<>&$#@!${}[];'");
            logger.Close();


            ILogRecord rec = logger.GetRecord(id, false);
            assert.IsEqual(id, rec.Id);

            assert.Write("test get record after package");
            id = logger.Info("helloworld");
            logger.Close();

            SingletonLogger.Backup(logger.Setting.LogPath, filename, postfix);
            assert.IsNull(logger.GetRecord(id, false));
            assert.IsEqual(id, logger.GetRecord(id, true).Id);


            ILogReport report = logger.GetRecentReport(0);
            assert.Write(report.Serialize());
            assert.IsEqual(0, report.Document.Records.Count);

            report = logger.GetRecentReport(Pixysoft.DateTimeCoder.Instance.DayToMilliSecond(1));
            assert.Write(report.Serialize());
            assert.IsTrue(report.Document.Records.Count > 0);
            assert.Write(report.Document.Records.Count);

            report = logger.GetReport(id);
            assert.IsNotNull(report.Record);
            assert.IsEqual(id, report.Record.Id);

            report = logger.GetReport(id, 0);
            assert.IsEqual(1, report.Document.Records.Count);
            report = logger.GetReport(id, DateTimeCoder.Instance.DayToMilliSecond(1));
            assert.IsTrue(report.Document.Records.Count > 0);
            assert.Write(report.Document.Records.Count);

            logger.Package();
        }
    }
}
